package main

import (
	"fmt"
	"regexp"
)

func main() {
	r := regexp.MustCompile("Hello")
	// The FindString function finds a string, and returns after the first match.
	fmt.Printf("%v\n", r.FindString("Hello Regular Expressions. Hullo again."))
	// When FindString does not find a string that matches the regular expression,
	// it will return the empty string.
	// BE AWARE that the empty string might also be the result of a valid match.
	r = regexp.MustCompile("Hxllo")
	fmt.Printf("%v\n", r.FindString("Hello Regular Expressions."))

	r = regexp.MustCompile("H")
	// FindAllString could find more matches
	fmt.Printf("%v\n", r.FindAllString("Hello Regular Expressions. Hola!", 1))

	// . matches any character
	r = regexp.MustCompile(".at")
	fmt.Printf("%v\n", r.FindString("The cat sat on the mat."))

	s := "Nobody expects the Spanish inquisition."
	r = regexp.MustCompile(`e.`)
	res := r.FindAllString(s, -1) // all matches
	fmt.Printf("%v", res)
	res = r.FindAllString(s, 2) // find 2 or less matches
	fmt.Printf("%v\n", res)

	// Literal Special Characters
	// backslash : it must be escaped twice in the regex and once in the string.
	r = regexp.MustCompile(`C\:`)
	if r.MatchString("Working on Drive C:\\") {
		fmt.Println("Matches.")
	} else {
		fmt.Println("No match.")
	}

	r = regexp.MustCompile(`\.`)
	if r.MatchString("Short.") {
		fmt.Println("Has a dot.")
	} else {
		fmt.Println("Has no dot.")
	}

	// Anchor and Boundaries

	// ^ denotes begin-of-line
	r = regexp.MustCompile(`^N`)
	fmt.Println(r.FindString("Never say never.")) // Does we have and 'N' at the beginning?
	// The $ denotes an end-of-line
	r = regexp.MustCompile(`\w+$`)
	fmt.Println(r.FindString("Hello Nowcoder"))
}
